package com.vtuzx.kingdee.xqc_core.service.impl;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_core.bean.kingdee.KingdeeAccount;
import com.vtuzx.kingdee.xqc_core.bean.kingdee.VtuzxKingdeeCloudClient;
import com.vtuzx.kingdee.xqc_core.dao.StoreTokenDao;
import com.vtuzx.kingdee.xqc_core.service.IVtuzxKingdeeService;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 金蝶服务
 */
@Service
public class VtuzxKingdeeServiceImpl implements IVtuzxKingdeeService {
    /** 日志 */
    private static final Logger _logger = LoggerFactory.getLogger(VtuzxKingdeeServiceImpl.class);

    /** 店铺TokenDao */
    @Autowired
    private StoreTokenDao storeTokenDao;

//    @Autowired
//    private CustomerDao customerDao;

    @Override
    public KingdeeAccount getAccount() {
        return storeTokenDao.getKingdeeAccount();
    }

    public KingdeeAccount getAccountYX() {
        return storeTokenDao.getKingdeeAccountYX();
    }

    public  KingdeeAccount getAccountYeXiang(){
        return  storeTokenDao.getKingdeeAccountYeXiang();
    }

    /**
     * 根据当前中台用户进行登录
     * @return
     */
    public  KingdeeAccount getAccountEveryOne(ObjectId userId, String name){
//        Document doc = storeTokenDao.getKingdeeConfigYeXiang();
//        Document customerDoc = customerDao.findOne(Criteria.where("_id").is(userId));
//        return new KingdeeAccount(
//                VtuzxUtil.getString(doc,"url"),
//                VtuzxUtil.getString(doc,"dataCenter"),
//                VtuzxUtil.getString(customerDoc,"kingdeeAccount"),
//                VtuzxUtil.getString(customerDoc,"kingdeePassword"),
//                VtuzxUtil.getInt(doc,"lang"));
        return null;
    }

    @Override
    public VtuzxKingdeeCloudClient loginKingdee() throws VtuzxException {
        KingdeeAccount account = getAccount();
        _logger.info("kingdee login account:" + account.getAccount() + ", password:" + account.getPassword());
        return new VtuzxKingdeeCloudClient(account).login();
    }

    @Override
    public VtuzxKingdeeCloudClient loginKingdeeYeXiang() throws VtuzxException {
        KingdeeAccount account = getAccountYeXiang();
        _logger.info("kingdee login account:" + account.getAccount() + ", password:" + account.getPassword());
        return new VtuzxKingdeeCloudClient(account).login();
    }

    @Override
    public VtuzxKingdeeCloudClient loginKingdeeYX() throws VtuzxException {
        KingdeeAccount account = getAccountYX();
        _logger.info("kingdee login account:" + account.getAccount() + ", password:" + account.getPassword());
        return new VtuzxKingdeeCloudClient(account).login();
    }



    public VtuzxMap getConfig() {
        Document config = storeTokenDao.getKingdeeConfig();
        return new VtuzxMap(config);
    }


    @Override
    public List<VtuzxMap> save(VtuzxKingdeeCloudClient client, String formId, VtuzxMap param) throws Exception {
        return client.save(formId, param);
    }

    @Override
    public List<VtuzxMap> save(VtuzxKingdeeCloudClient client, String formId, Map<String, Object> param) throws Exception {
        VtuzxMap p = new VtuzxMap(param);
        return client.save(formId, p);
    }

    @Override
    public List<VtuzxMap> push(VtuzxKingdeeCloudClient client, String formId, VtuzxMap param) throws Exception {
        return client.push(formId, param);
    }

    @Override
    public List<VtuzxMap> push(VtuzxKingdeeCloudClient client, String formId, Map<String, Object> param) throws Exception {
        VtuzxMap p = new VtuzxMap(param);
        return client.push(formId, p);
    }

    @Override
    public List<VtuzxMap> submit(VtuzxKingdeeCloudClient client, String formId, String orgId, String billNo) throws VtuzxException {
        return client.submit(formId, new VtuzxMap("CreateOrgId", orgId).append("Numbers", billNo));
    }

    @Override
    public List<VtuzxMap> audit(VtuzxKingdeeCloudClient client, String formId, String orgId, String billNo) throws VtuzxException {
        return client.audit(formId, new VtuzxMap("CreateOrgId", orgId).append("Numbers", billNo));
    }

    @Override
    public List<VtuzxMap> audit(VtuzxKingdeeCloudClient client, String formId, String orgId, String billNo, String interationFlags) throws VtuzxException {
        return client.audit(formId, new VtuzxMap("CreateOrgId", orgId).append("Numbers", billNo).append("InterationFlags", interationFlags));
    }

    @Override
    public List<VtuzxMap> toRelease(VtuzxKingdeeCloudClient client, String formId, String status, String orgId, String billNo) throws VtuzxException {
        return client.toRelease(formId, status, new VtuzxMap("CreateOrgId", orgId).append("Numbers", billNo));
    }

    @Override
    public List<VtuzxMap> toRelease(VtuzxKingdeeCloudClient client, String formId, String status, String orgId, String billNo, String interationFlags) throws VtuzxException {
        return client.toRelease(formId, status, new VtuzxMap("CreateOrgId", orgId).append("Numbers", billNo).append("InterationFlags", interationFlags));
    }

    @Override
    public List<VtuzxMap> saveAttachment(VtuzxKingdeeCloudClient client, String type, String fId, String fNumber, String fileId, String fileName, int fileSize, String ext, boolean allowDownload) throws Exception {
        VtuzxMap model = new VtuzxMap();
        model.append("FID", 0);
        model.append("FBILLTYPE", type);
        model.append("FINTERID", fId);
        model.append("FBILLNO", fNumber);
        model.append("FENTRYKEY", " ");
        model.append("FENTRYINTERID", -1);
        model.append("FFILEID", fileId);
        model.append("FFILESTORAGE","1");
        model.append("FATTACHMENTNAME", fileName);
        model.append("FEXTNAME", "." + ext);
        model.append("FATTACHMENTSIZE", new BigDecimal(fileSize).divide(BigDecimal.valueOf(1024), 2, RoundingMode.HALF_UP).doubleValue());
        model.append("FBILLSTATUS", "A");
        model.append("FALIASFILENAME", "");
        model.append("FIsAllowDownLoad", allowDownload);
        model.append("FCREATEMEN", new VtuzxMap("FUSERID", client.get("UserId")));
        model.append("FCREATETIME", VtuzxUtil.formatYmdHmsF(new Date()));
        VtuzxMap param = new VtuzxMap("Creator", "Vtuzx")
                .append("NeedUpDateFields", new ArrayList<>())
                .append("Model", model);
        return client.save("BOS_Attachment", param);
    }

    @Override
    public List<VtuzxMap> query(VtuzxKingdeeCloudClient client, String formId, List<String> fieldList, String cond, String order, int start, int limit) throws Exception {
        List<String> fillFieldList = fieldList == null? new ArrayList<>(): new ArrayList<>(fieldList);
        return client.query(formId, fillFieldList, cond, order, start, limit);
    }

    @Override
    public int orderUpdate(VtuzxKingdeeCloudClient client, String formId, String billNo, String updateTime) throws VtuzxException {

        return client.orderUpdate(formId,billNo,updateTime);

    }

    @Override
    public VtuzxKingdeeCloudClient loginKingdeeEveryClient(ObjectId userId, String name) throws VtuzxException {
        KingdeeAccount account = getAccountEveryOne(userId,name);
        _logger.info("kingdee login account:" + account.getAccount() + ", password:" + account.getPassword());
        return new VtuzxKingdeeCloudClient(account).login();
    }

}
